/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Data.BoPhan;
import Data.DatVeOnline;
import Data.GheNgoi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author bienphongvnn
 */
public class GheNgoiModel {

    public static ArrayList<GheNgoi> ListGheNgoi() {
        ArrayList<GheNgoi> ds = new ArrayList<GheNgoi>();
        Connection con = ConnectionProvider.Connect();
        try {
            String sql = "SELECT * FROM ghengoi";
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                GheNgoi ghe = new GheNgoi();
                ghe.SoHieuGhe = rs.getInt("SoHieuGhe");
                ghe.TinhTrang = rs.getString("TinhTrang");
                ghe.MaToa = rs.getString("MaToa");
                ghe.MaTau = rs.getString("MaTau");
                ghe.MaKhachHang = rs.getString("MaKhachHang");
                ghe.GiaVe = rs.getDouble("GiaVe");
                ds.add(ghe);
            }
            con.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {

        }
        return ds;
    }

    public static void Them(GheNgoi n) throws SQLException {
        Connection conn = ConnectionProvider.Connect();
        try {

            String sql = "INSERT INTO ghengoi(SoHieuGhe, TinhTrang, MaToa, MaTau, MaKhachHang, GiaVe) VALUES(?,?,?,?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1, n.SoHieuGhe);
            pst.setString(2, n.TinhTrang);
            pst.setString(3, n.MaToa);
            pst.setString(4, n.MaTau);
            pst.setString(5, n.MaKhachHang);
            pst.setDouble(6, n.GiaVe);
            pst.executeUpdate();
            pst.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
        } finally {
            conn.close();
        }
    }

    //Tim kiếm
    public static boolean TimKiemTatCa(int sohieu, String matoa, String matau) {
        ArrayList<GheNgoi> ds = new ArrayList<GheNgoi>();
        Connection conn = ConnectionProvider.Connect();
        try {
            String sql = "Select  * from ghengoi where SoHieuGhe = '" + sohieu + "' and MaToa = '" + matoa + "' and MaTau = '" + matau + "'";
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                GheNgoi ghe = new GheNgoi();
                ghe.SoHieuGhe = rs.getInt("SoHieuGhe");
                ghe.TinhTrang = rs.getString("TinhTrang");
                ghe.MaToa = rs.getString("MaToa");
                ghe.MaTau = rs.getString("MaTau");
                ghe.MaKhachHang = rs.getString("MaKhachHang");
                ghe.GiaVe = rs.getDouble("GiaVe");
                ds.add(ghe);
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } 
        finally {
            if (ds.size() >= 1) {
                return true;
            } else {
                return false;
            }
        }
    }
}
